
/**
 * @fileoverview Defines namespace for 'analytics.providers' package.
 * @link http://google-styleguide.googlecode.com/svn/trunk/javascriptguide.xml
 * @link https://developers.google.com/closure/compiler/docs/js-for-compiler
 */


/**
 * Defines namespace for 'analytics.providers' package.
 * @namespace Defines namespace for 'analytics.providers' package.
 */
analytics.providers = analytics.providers || {};



/**
 * @constructor
 */
analytics.providers.BaseProvider = function() {

  /**
   * @param {...*} var_args A variable number of parameters.
   * @return {boolean} Always returns false for prevent default link behavior.
   */
  this.authorize = function(var_args) {return false;};

  this.unauthorize = function() {};

  this.getDateRange = function() {
    /** @type {Element} */
    var startDateInput = dom.getElementById('start-date');
    /** @type {Element} */ var endDateInput = dom.getElementById('end-date');
    /** @type {number} */ var day = 60 * 60 * 24 * 1000;
    /** @type {!Date} */ var today = new Date;

    if (!startDateInput.value) {
      /** @type {string} */ var startDate = formatters.DateFormatter.format(
          new Date(+today - day * 7), 'YYYY-MM-dd');
      startDateInput.value = startDate;
      startDateInput.onchange = function() {
        dom.getElementById('reports-listbox').onchange(null);
      };
    }

    if (!endDateInput.value) {
      /** @type {string} */
      var endDate = formatters.DateFormatter.format(today, 'YYYY-MM-dd');
      endDateInput.value = endDate;
      endDateInput.setAttribute('max', endDate);
      endDateInput.onchange = function() {
        dom.getElementById('reports-listbox').onchange(null);
      };
    }

    return [startDateInput.value, endDateInput.value];
  };

  this.handleAuthorized = function(profiles, username) {
    //console.log(profiles);
    dom.getElementById('progress').style.display = 'none';
    window.scrollTo(0, 1);
    initUserMenu_(username);

    /** @type {Element} */
    var authorizeButton = dom.getElementById('auth-container');
    /** @type {Element} */
    var profilesListbox = dom.getElementById('profiles-listbox');
    /** @type {Element} */
    var reportsListbox = dom.getElementById('reports-listbox');
    /** @type {Element} */ var reportContainer = dom.getElementById('report');
    authorizeButton.style.display = 'none';
    profilesListbox.style.display = '';
    reportContainer.innerHTML = '';
    profilesListbox.options.length = 0;
    profilesListbox.options[0] = new Option('Profiles');
    for (/** @type {number} */ var i = 0; i < profiles.length; i++) {
      /** @type {!Object} */ var profile = profiles[i];
      /** @type {Option} */
      var option = new Option(profile['name'], profile['id']);
      profilesListbox.options[i + 1] = option;
    }

    profilesListbox.onchange = function() {
      window.scrollTo(0, 1);
      reportsListbox.style.display = '';
      reportContainer.innerHTML = '';
      /** @type {string} */ var profileId = profilesListbox.value;
      /** @type {!Array.<Object>} */ var reports = self_.getReports(profileId);
      reportsListbox.options.length = 0;
      for (i = 0; i < reports.length; i++) {
        /** @type {Object} */ var report = reports[i];
        option = new Option(report['name'], report['id']);
        reportsListbox.options[i] = option;
        if (!i) {
          self_.loadReport(profileId, report['id'], loadReportCallback_);
        }
      }
      reportsListbox.onchange = function() {
        window.scrollTo(0, 1);
        /** @type {string} */ var reportId = reportsListbox.value;
        self_.loadReport(profileId, reportId, loadReportCallback_);
      };
    };
  };

  this.handleUnAuthorized = function() {
    dom.getElementById('progress').style.display = 'none';
    var authorizeButton = dom.getElementById('auth-container');
    var profilesListbox = dom.getElementById('profiles-listbox');
    var reportsListbox = dom.getElementById('reports-listbox');
    var reportContainer = dom.getElementById('report');
    profilesListbox.style.display = 'none';
    reportsListbox.style.display = 'none';
    reportContainer.innerHTML = '';
    authorizeButton.style.display = 'block';
    var button = authorizeButton.getElementsByTagName('BUTTON')[0];
    button.onclick = function() {
      dom.getElementById('progress').style.display = 'block';
      return self_.authorize(1);
    };
  };

  this.getReports = function(profileId) {return []};
  this.loadReport = function(profileId, reportId, callback) {};

  /**
   * @param {!Array.<Array>} data The data array.
   * @private
   */
  function loadReportCallback_(data) {
    //console.log(data);
    dom.getElementById('date-range').style.display = 'block';
    /** @type {Element} */ var container = dom.getElementById('report');
    container.style.display = 'block';
    container.innerHTML = '';
    /** @type {Node} */
    var chartContainer = container.appendChild(dom.createElement('DIV'));
    drawChart_(data, chartContainer);
    dom.events.addEventListener(window, 'orientationchange', function() {
      drawChart_(data, chartContainer);
    });

    /** @type {!analytics.DataGrid} */
    var grid = new analytics.DataGrid(container);
    grid.draw(data);
  }

  function drawChart_(data, container) {
    /** @type {string} */
    var type = data['chart'] && data['chart']['type'] || 'line';
    container.innerHTML = '';
    container.className = 'chart';
    if ('line' == type) {
      drawLineChart_(data, container);
    } else if ('donut' == type) {
      drawDonutChart_(data, container);
    }
  }

  function drawDonutChart_(data, container) {
    /** @type {!Array} */ var rows = [].concat(data['rows']);
    rows.sort(function(a, b) {return +b[1] - +a[1]});
    /** @type {!Array.<Array>} */ var prepared = [[], []];
    /** @type {number} */ var length = Math.min(5, rows.length);
    for (/** @type {number} */ var i = 0; i < length; i++) {
      /** @type {Array} */ var row = rows[i];
      prepared[0].push(row[0]);
      prepared[1].push(+row[1]);
    }
    /** @type {!charts.DonutChart} */
    var chart = new charts.DonutChart(container);
    chart.draw(prepared);
  }

  function drawLineChart_(data, container) {
    /** @type {!Array} */ var columns = [].concat(data['columns']);
    /** @type {!Array} */ var rows = [].concat(data['rows']);
    /** @type {!Array} */ var prepared = [columns.splice(0, 3)];
    for (/** @type {number} */ var i = 0; i < rows.length; i++) {
      /** @type {!Array} */ var row = [].concat(rows[i]);
      prepared.push(row.splice(0, 3));
    }
    /** @type {!charts.LineChart} */
    var chart = new charts.LineChart(container);
    chart.draw(prepared, {'hAxis': false, 'vAxis': false});
  }

  function initUserMenu_(username) {
    dom.getElementById('user-name').innerHTML = username;
    dom.getElementById('sign-out-button').onclick = function() {
      self_.unauthorize();
    };
    dom.getElementById('header').style.display = 'block';
    dom.getElementById('content').style.display = 'block';
  }

  /**
   * The reference to current class instance. Used in private methods.
   * @type {!analytics.providers.BaseProvider}
   * @private
   */
  var self_ = this;
};
